<%@ page contentType="text/html;charset=UTF-8" %>
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>

    <head>
      <script>
        //检索数据集
        var dataList = {
          "review_content": null,
          "product_id": null,
          "user_id": null
        };
        $(function () {
          /******
           * event
           ******/
          //点击查询按钮时
          $("#btn_review_submit").click(function () {
            var review_content = $.trim($("#input_review_content").val());
            var product_id = $.trim($("#input_product_id").val());
            var user_id = $.trim($("#input_user_id").val());

            //封装数据
            dataList.review_content = encodeURI(review_content);
            dataList.product_id = product_id === "" ? null : product_id;
            dataList.user_id = user_id === "" ? null : user_id;

            getData($(this), "admin/review/0/10", dataList);
          });

          //点击刷新按钮时
          $("#btn_review_refresh").click(function () {
            //清除数据
            dataList.review_content = null;
            dataList.product_id = null;
            dataList.user_id = null;
            //获取数据
            getData($(this), "admin/review/0/10", null);
          });

          //点击table中的数据时
          $("#table_review_list").find(">tbody>tr").click(function () {
            trDataStyle($(this));
          });

          // 全选/取消全选
          $("#cbx_select_all").click(function () {
            var isChecked = $(this).prop("checked");
            $("input.cbx_select").prop("checked", isChecked);
          });

          // 批量删除按钮点击事件
          $("#btn_review_delete_batch").click(function () {
            var selectedReviews = $("input.cbx_select:checked:not(#cbx_select_all)");
            if (selectedReviews.length === 0) {
              alert("请选择要删除的评价");
              return;
            }

            if (confirm("确定要删除选中的 " + selectedReviews.length + " 条评价吗？此操作不可恢复！")) {
              var reviewIds = [];
              selectedReviews.each(function () {
                var reviewId = $(this).attr("id").substring("cbx_review_select_".length);
                reviewIds.push(reviewId);
              });

              deleteReviews(reviewIds);
            }
          });
        });

        //获取评价数据
        function getData(object, url, dataObject) {
          var table = $("#table_review_list");
          var tbody = table.children("tbody").first();
          $.ajax({
            url: url,
            type: "get",
            data: dataObject,
            traditional: true,
            success: function (data) {
              //清空原有数据
              tbody.empty();
              //设置样式
              $(".loader").css("display", "none");
              object.attr("disabled", false);
              //显示评价统计数据
              $("#review_count_data").text(data.reviewCount);
              if (data.reviewList.length > 0) {
                for (var i = 0; i < data.reviewList.length; i++) {
                  var review_id = data.reviewList[i].review_id;
                  var review_content = data.reviewList[i].review_content;
                  var review_createDate = data.reviewList[i].review_createDate;
                  var user_name = data.reviewList[i].review_user.user_name;
                  var user_id = data.reviewList[i].review_user.user_id;
                  var product_name = data.reviewList[i].review_product.product_name;
                  var product_id = data.reviewList[i].review_product.product_id;

                  //显示评价数据
                  tbody.append("<tr><td><input type='checkbox' class='cbx_select' id='cbx_review_select_" + review_id + "'><label for='cbx_review_select_" + review_id + "'></label></td><td title='" + review_content + "'>" + review_content + "</td><td title='" + review_createDate + "'>" + review_createDate + "</td><td title='" + user_name + "'><a href='javascript:void(0)' onclick='getUser(" + user_id + ")'>" + user_name + "</a></td><td title='" + product_name + "'><a href='javascript:void(0)' onclick='getProduct(" + product_id + ")'>" + product_name + "</a></td><td><span class='td_special' title='查看评价详情'><a href='javascript:void(0);' onclick='getChildPage(this)'>详情</a></span> <span class='td_special' title='删除评价'><a href='javascript:void(0);' onclick='deleteReview(this)'>删除</a></span></td><td hidden class='review_id'>" + review_id + "</td></tr>");
                }
                //绑定事件
                tbody.children("tr").click(function () {
                  trDataStyle($(this));
                });
                //分页
                var pageUtil = {
                  index: data.pageUtil.index,
                  count: data.pageUtil.count,
                  total: data.pageUtil.total,
                  totalPage: data.totalPage
                };
                createPageDiv($(".loader"), pageUtil);
              }
            },
            beforeSend: function () {
              $(".loader").css("display", "block");
              object.attr("disabled", true);
            },
            error: function () {
              alert("加载评价数据失败，请稍后重试！");
            }
          });
        }

        //获取评价子界面
        function getChildPage(obj) {
          //设置样式
          $("#div_home_title").children("span").text("评价详情");
          document.title = "管理后台 - 评价详情";
          //ajax请求页面
          ajaxUtil.getPage("review/" + $(obj).parents("tr").find(".review_id").text(), null, true);
        }

        //获取用户信息
        function getUser(userId) {
          //设置样式
          $("#div_home_title").children("span").text("用户详情");
          document.title = "管理后台 - 用户详情";
          //ajax请求页面
          ajaxUtil.getPage("user/" + userId, null, true);
        }

        //获取商品信息
        function getProduct(productId) {
          //设置样式
          $("#div_home_title").children("span").text("产品详情");
          document.title = "管理后台 - 产品详情";
          //ajax请求页面
          ajaxUtil.getPage("product/" + productId, null, true);
        }

        //获取页码数据
        function getPage(index) {
          getData($(this), "admin/review/" + index + "/10", dataList);
        }

        // 刷新评价列表函数
        function refreshReviewList() {
          //清除数据
          dataList.review_content = null;
          dataList.product_id = null;
          dataList.user_id = null;
          //获取数据
          getData($("#btn_review_refresh"), "admin/review/0/10", null);
        }

        //删除单个评价
        function deleteReview(obj) {
          var reviewId = $(obj).parents("tr").find(".review_id").text();

          if (confirm("确定要删除这条评价吗？此操作不可恢复！")) {
            $.ajax({
              url: "admin/review/delete/" + reviewId,
              type: "DELETE",
              success: function (data) {
                var dataObj = JSON.parse(data);
                if (dataObj.success) {
                  alert("删除成功！");
                  refreshReviewList();
                } else {
                  alert("删除失败：" + dataObj.message);
                }
              },
              error: function () {
                alert("操作失败，请稍后再试！");
              }
            });
          }
        }

        //批量删除评价
        function deleteReviews(reviewIds) {
          $.ajax({
            url: "admin/review/delete/batch",
            type: "DELETE",
            data: { "review_id_list[]": reviewIds },
            traditional: true,
            success: function (data) {
              var dataObj = JSON.parse(data);
              if (dataObj.success) {
                alert(dataObj.message);
                refreshReviewList();
              } else {
                alert("删除失败：" + dataObj.message);
              }
            },
            error: function (xhr, status, error) {
              console.log("删除失败:", xhr, status, error);
              alert("操作失败，请稍后再试！");
            }
          });
        }
      </script>
      <style rel="stylesheet">
        #lbl_review_content,
        #lbl_product_id,
        #lbl_user_id {
          width: 65px;
        }

        .frm_danger {
          background-color: #dc3545;
          color: white;
          margin-left: 10px;
        }

        .frm_danger:hover {
          background-color: #c82333;
        }

        .td_special a {
          margin-right: 10px;
        }
      </style>
    </head>

    <body>
      <div class="frm_div text_info">
        <div class="frm_group">
          <label class="frm_label" id="lbl_review_content" for="input_review_content">评价内容</label>
          <input class="frm_input" id="input_review_content" type="text" maxlength="50" />
        </div>
        <div class="frm_group">
          <label class="frm_label" id="lbl_product_id" for="input_product_id">商品ID</label>
          <input class="frm_input" id="input_product_id" type="text" maxlength="10" />
        </div>
        <div class="frm_group">
          <label class="frm_label" id="lbl_user_id" for="input_user_id">用户ID</label>
          <input class="frm_input" id="input_user_id" type="text" maxlength="10" />
          <input class="frm_btn" id="btn_review_submit" type="button" value="查询" />
        </div>
        <div class="frm_group_last">
          <input class="frm_btn frm_refresh" id="btn_review_refresh" type="button" value="刷新评价列表" />
          <input class="frm_btn frm_danger" id="btn_review_delete_batch" type="button" value="批量删除" />
        </div>
      </div>
      <div class="data_count_div text_info">
        <svg class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2522"
          width="16" height="16">
          <path
            d="M401.976676 735.74897c-88.721671 0-172.124196-34.635845-234.843656-97.526197-62.724577-62.86784-97.271394-146.453537-97.271394-235.358379s34.546817-172.490539 97.276511-235.361449c62.715367-62.887282 146.117892-97.522104 234.838539-97.522104 88.719624 0 172.135452 34.633798 234.881518 97.522104 62.704111 62.875003 97.235578 146.4607 97.235578 235.361449 0 88.901773-34.530444 172.487469-97.231485 235.358379C574.112128 701.116195 490.6963 735.74897 401.976676 735.74897zM401.976676 121.204479c-75.012438 0-145.533584 29.290093-198.572568 82.474386-109.585861 109.834524-109.585861 288.539602-0.004093 398.36901 53.043077 53.188386 123.564223 82.47848 198.577684 82.47848 75.015507 0 145.553027-29.291117 198.620663-82.47848C710.126918 492.220514 710.126918 313.511343 600.593246 203.678866 547.530726 150.496619 476.992183 121.204479 401.976676 121.204479z"
            p-id="2523" fill="#FF7874"></path>
          <path
            d="M932.538427 958.228017c-6.565533 0-13.129019-2.508123-18.132986-7.52437L606.670661 642.206504c-9.989515-10.014074-9.969049-26.231431 0.045025-36.220946s26.230408-9.969049 36.220946 0.045025l307.73478 308.497143c9.989515 10.014074 9.969049 26.231431-0.045025 36.220946C945.627537 955.735244 939.081447 958.228017 932.538427 958.228017z"
            p-id="2524" fill="#FF7874"></path>
        </svg>
        <span class="data_count_title">查看合计</span>
        <span>评价总数:</span>
        <span class="data_count_value" id="review_count_data">${requestScope.reviewCount}</span>
        <span class="data_count_unit">条</span>
      </div>
      <div class="table_normal_div">
        <table class="table_normal" id="table_review_list">
          <thead class="text_info">
            <tr>
              <th><input type="checkbox" class="cbx_select" id="cbx_select_all"><label for="cbx_select_all"></label>
              </th>
              <th>评价内容</th>
              <th>评价日期</th>
              <th>用户名</th>
              <th>商品名称</th>
              <th>操作</th>
              <th hidden>评价ID</th>
            </tr>
          </thead>
          <tbody>
            <c:forEach items="${requestScope.reviewList}" var="review">
              <tr>
                <td><input type="checkbox" class="cbx_select" id="cbx_review_select_${review.review_id}"><label
                    for="cbx_review_select_${review.review_id}"></label></td>
                <td title="${review.review_content}">${review.review_content}</td>
                <td title="${review.review_createDate}">${review.review_createDate}</td>
                <td title="${review.review_user.user_name}">
                  <a href="javascript:void(0)"
                    onclick="getUser(${review.review_user.user_id})">${review.review_user.user_name}</a>
                </td>
                <td title="${review.review_product.product_name}">
                  <a href="javascript:void(0)"
                    onclick="getProduct(${review.review_product.product_id})">${review.review_product.product_name}</a>
                </td>
                <td>
                  <span class="td_special" title="查看评价详情"><a href='javascript:void(0)'
                      onclick='getChildPage(this)'>详情</a></span>
                  <span class="td_special" title="删除评价"><a href='javascript:void(0)'
                      onclick='deleteReview(this)'>删除</a></span>
                </td>
                <td hidden class="review_id">${review.review_id}</td>
              </tr>
            </c:forEach>
          </tbody>
        </table>
        <%@ include file="include/page.jsp" %>
          <div class="loader"></div>
      </div>
    </body>

    </html>